package br.net.woodstock.tickettoride.client.web.taglib;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import br.net.woodstock.rockframework.utils.ConditionUtils;
import br.net.woodstock.tickettoride.client.TicketHash;
import br.net.woodstock.tickettoride.client.TicketRequest;
import br.net.woodstock.tickettoride.client.TicketSession;
import br.net.woodstock.tickettoride.client.common.DefaultTicketRequest;
import br.net.woodstock.tickettoride.client.common.MapTicketConverter;
import br.net.woodstock.tickettoride.client.common.TicketSessionHolder;
import br.net.woodstock.tickettoride.client.web.TicketToRideFilter;

public class URLTag extends SimpleTagSupport {

	private static final Integer			DEFAULT_EXPIRES	= Integer.valueOf(5);

	private static final String				START_PARAM		= "?";

	private static final String				PARAM_SEPARATOR	= "&";

	private static final String				VALUE_SEPARATOR	= "=";

	private static final MapTicketConverter	CONVERTER		= new MapTicketConverter();

	private String							url;

	private String							destination;

	private String							expires;

	private String							var;

	private Map<String, String>				params;

	@Override
	public void doTag() throws JspException, IOException {
		this.params = new HashMap<String, String>();
		this.getJspBody().invoke(this.getJspContext().getOut());

		String hash = this.sendTicket();
		StringBuilder builder = new StringBuilder();
		builder.append(this.url);
		if (!((this.url.endsWith(URLTag.START_PARAM)) || (this.url.endsWith(URLTag.PARAM_SEPARATOR)))) {
			builder.append(URLTag.START_PARAM);
		}
		builder.append(TicketToRideFilter.TICKET_HASH_PARAMETER);
		builder.append(URLTag.VALUE_SEPARATOR);
		builder.append(hash);

		if (ConditionUtils.isNotEmpty(this.var)) {
			this.getJspContext().setAttribute(this.var, builder.toString());
		} else {
			this.getJspContext().getOut().write(builder.toString());
		}
	}

	private String sendTicket() {
		byte[] data = URLTag.CONVERTER.toBytes(this.params);
		Integer expires = URLTag.DEFAULT_EXPIRES;
		if (ConditionUtils.isNotEmpty(this.expires)) {
			expires = Integer.valueOf(this.expires);
		}
		TicketRequest request = new DefaultTicketRequest(this.destination, expires, data);
		TicketSession session = TicketSessionHolder.getInstance().getSession();
		TicketHash hash = session.send(request);
		return hash.getHash();
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(final String url) {
		this.url = url;
	}

	public String getDestination() {
		return this.destination;
	}

	public void setDestination(final String destination) {
		this.destination = destination;
	}

	public String getExpires() {
		return this.expires;
	}

	public void setExpires(final String expires) {
		this.expires = expires;
	}

	public String getVar() {
		return this.var;
	}

	public void setVar(final String var) {
		this.var = var;
	}

	public Map<String, String> getParams() {
		return this.params;
	}

	public void setParams(final Map<String, String> params) {
		this.params = params;
	}

}
